package jp.ac.aiit.spg.web.todo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;

import org.seasar.teeda.extension.annotation.convert.DateTimeConverter;

import jp.ac.aiit.spg.AbstractSpgPage;
import jp.ac.aiit.spg.common.SpgAssignStatus;
import jp.ac.aiit.spg.entity.SpgTask;
import jp.ac.aiit.spg.service.SpgService;
import jp.ac.aiit.spg.web.login.LoginPage;

public class TodoAssignedPage extends AbstractSpgPage {
	
	@EJB
	private SpgService ss;
	private List<SpgTask> tasks = new ArrayList<SpgTask>();

	private int todoAssingedIndex;
	private List<TodoAssignedItem> todoAssingedItems = new ArrayList<TodoAssignedItem>();
	private String projectName;
	private String wbsNumber;
	private String taskName;
	private int workingTime;
	@DateTimeConverter(pattern = "yyyy/MM/dd")
	private Date startTimePlanned;
	@DateTimeConverter(pattern = "yyyy/MM/dd")
	private Date finishTimePlanned;
	private String assignStatement;
	private int taskId;
	

	public Class initialize() {
		
		// ログインチェック
		if (this.isLoggedIn() == false) {
			return LoginPage.class;
		}else{
			todoAssingedItems.clear();
			String[] sortOrder = {"startTimePlanned","wbs"};
			tasks =ss.listUserTasksByAssignStatus(getLoginUserId(), SpgAssignStatus.ASSIGNED, sortOrder);
			setToDoInfo();
		}
		return null;
	}

	public Class prerender() {
		
		return null;
	}

	public Class doTaskSortStartTimePlanned(){
		todoAssingedItems.clear();
		String[] sortOrder = {"startTimePlanned","wbs"};;

		tasks =ss.listUserTasksByAssignStatus(getLoginUserId(), SpgAssignStatus.ASSIGNED, sortOrder);
		setToDoInfo();
		return null;
		
	}
	public Class doTaskSortWbs(){
		todoAssingedItems.clear();
		String[] sortOrder = {"wbs","startTimePlanned"};;
	
		tasks =ss.listUserTasksByAssignStatus(getLoginUserId(), SpgAssignStatus.ASSIGNED, sortOrder);
		setToDoInfo();
		return null;	
	}
	private void setToDoInfo() {
		
		for (SpgTask t : tasks) {
			TodoAssignedItem item = new TodoAssignedItem();
			item.setProjectName(t.getWbs().getProject().getName());
			item.setTaskId(t.getId());
			item.setTaskName(t.getName());
			item.setWbsNumber(t.getWbsNumber());
			item.setWorkingTime(t.getWorkingTime());
			item.setStartTimePlanned(t.getStartTimePlanned());
			item.setFinishTimePlanned(t.getFinishTimePlanned());
			item.setAssignStatement(t.getAssignment().toString());
			todoAssingedItems.add(item);
		}
	}

	public int getTodoAssingedIndex() {
		return todoAssingedIndex;
	}

	public void setTodoAssingedIndex(int todoAssingedIndex) {
		this.todoAssingedIndex = todoAssingedIndex;
	}

	public List<TodoAssignedItem> getTodoAssingedItems() {
		return todoAssingedItems;
	}

	public void setTodoAssingedItems(List<TodoAssignedItem> todoAssingedItems) {
		this.todoAssingedItems = todoAssingedItems;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getWbsNumber() {
		return wbsNumber;
	}

	public void setWbsNumber(String wbsNumber) {
		this.wbsNumber = wbsNumber;
	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public int getWorkingTime() {
		return workingTime;
	}

	public void setWorkingTime(int workingTime) {
		this.workingTime = workingTime;
	}

	public Date getStartTimePlanned() {
		return startTimePlanned;
	}

	public void setStartTimePlanned(Date startTimePlanned) {
		this.startTimePlanned = startTimePlanned;
	}

	public Date getFinishTimePlanned() {
		return finishTimePlanned;
	}

	public void setFinishTimePlanned(Date finishTimePlanned) {
		this.finishTimePlanned = finishTimePlanned;
	}

	public String getAssignStatement() {
		return assignStatement;
	}

	public void setAssignStatement(String assignStatement) {
		this.assignStatement = assignStatement;
	}

	public int getTaskId() {
		return taskId;
	}

	public void setTaskId(int taskId) {
		this.taskId = taskId;
	}

}
